<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流水号管理</title>
    <#include "/pubHeadStyle.html">
</head>
<body>
<div class="tablesize roles" style="padding:10px">
    <table id="serNumber" lay-filter="serNumber"></table>
</div>

<script type="text/html" id="serNumTool">
    <div class="ccpui-enterSel">
        <div class="layui-btn-container">
            <a class="layui-btn layui-btn-sm" lay-event="query">
                <i class="layui-icon layui-icon-search"></i>
                <span>查询</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                <span>新增</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>
                <span>编辑</span>
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delSome">
                <i class="layui-icon layui-icon-delete"></i>
                <span>删除</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="export">
                <i class="layui-icon layui-icon-export"></i>
                <span>导出</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="import">
                <i class="layui-icon layui-icon-download-circle"></i>
                <span>导入</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                <span>重置</span>
            </a>
        </div>
        <form class="layui-form" action="" lay-filter="identity" style="margin-top:10px">
            <div class="table-top-filter">
                <div class="table-filter-alone">
                    <label class="layui-form-label">名称</label>
                    <input type="text" name="Q_name_SL" id="neme" class="layui-input filter-value"
                           placeholder="名称"/>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">别名</label>
                    <input type="text" name="Q_alias_SL" id="alias" class="layui-input filter-value"
                           placeholder="别名"/>
                </div>
            </div>
        </form>
    </div>
</script>
<script type="text/html" id="manageBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="detail">明细</a>
</script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','form', 'layedit', 'laydate', 'jquery', 'common', 'table', 'layer', 'totalFilter','commons'], function () {
        var form = layui.form, layer = layui.layer, totalFilter = layui.totalFilter;
        var table = layui.table;
        var $ = layui.$;
        var setter = layui.setter, common = layui.common;
        var commons = layui.commons;

        var tableH = commons.tabHeight();

        var index = parent.layer.getFrameIndex(window.name);
        $("#returnCol").on('click', function () {
            parent.layer.close(index);
        });
        table.render({
            elem: '#serNumber'
            , url: serverPath.workflowPath + setter.workflowLinkPath + "identity/list"
            , height: tableH
            ,defaultToolbar: ['filter', 'print']
            , toolbar: '#serNumTool'
            , page: true
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'identityName', title: '名称', align: "center",templet:function (d) {
                        return d.identityName.replace(/</g,"&lt;").replace(/>/g,"&gt;");
                    }
                }
                , {field: 'alias', title: '别名', align: "center"}
                , {field: 'rule', title: '规则', align: "center"}
                , {field: 'genType', title: '生成类型', sort: true, align: 'center',templet: function (d) {
                        if (d.genType == 1){
                            return '<span style="color: #15cc08;">每天生成</span>';
                        }else if (d.genType == 2){
                            return '<span style="color: #15cc08;">每月生成</span>';
                        }else if (d.genType == 3){
                            return '<span style="color: #15cc08;">每年生成</span>';
                        } else{
                            return '<span style="color: #cc0c00;">递增</span>';
                        }
                    }
                }
                , {field: 'noLength', title: '流水号长度', sort: true, align: 'center'}
                , {field: 'initValue', title: '初始值', sort: true, align: 'center'}
                , {fixed: 'right', title: '管理', toolbar: '#manageBar', width: 250, align: 'center'}
            ]]
            ,done:function () {
                common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel');
            }
        });
        totalFilter.init('serNumber');
        form.on('checkbox', function (data) {
            var tr = $(this).closest('tr'), tds = tr.find('td');
            var value = tds.eq(1).text();
            parent.$('#identityName').val(value);
        });

        function addOrUpdate(type,data){
            layer.open({
                type: 2,
                title: type == 'add'?"新增":'编辑',
                area: ['800px', '600px'],
                content:  serverPath.workflowPath +  setter.workflowLinkPath + "identity/view/commonAdd"+(type === 'add' ?  '' : '?id='+data)
            })
        }
        //头工具栏事件
        table.on('toolbar(serNumber)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("serNumber", "identity", {where: {}}, false);
                    break;
                case 'reset':
                    common.tableConditionRender("serNumber", "identity", {where: {}}, true);
                    break;
                case 'add':
                    addOrUpdate('add');
                    break;
                case 'edit':
                    if (data.length === 0){
                        layer.msg("还没有选择，请选择一项进行编辑！",{time: 2500, icon:5});
                        return false;
                    }
                    if (data.length !== 0 && data.length !== 1){
                        layer.msg("已经选择了多项，请选择一项进行编辑！",{time:2500,icon:5});
                        return false;
                    }
                    addOrUpdate('edit',data[0].id);
                    break;
                case 'delSome':
                    if(data.length === 0){
                        layer.msg("请选择记录!", {time: 2500, icon:5});
                        return false;
                    }
                    var ids = $.map(data,function(v){
                        return v.id;
                    });
                    layer.confirm('确定删除吗？',{skin: 'btn-class'}, function () {
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + 'identity/del',
                            type:'post',
                            dataType: 'json',
                            data: {ids: ids.toString()},
                            success: function (data) {
                               deleteSuccess(data);
                            }
                        })
                    });
                    break;
                case 'export':
                    if (data.length == 0) {
                        layer.confirm('还没有选择，是否导出全部？', {
                            btn: ['确定', '取消'] //按钮
                        }, function (index) {
                            layer.close(index);
                            var url = serverPath.workflowPath + setter.workflowLinkPath + 'identity/exportXml';
                            window.location.href = url;
                        });
                    } else {
                        layer.confirm('确定导出吗？', {
                            btn: ['确定', '取消'] //按钮
                        }, function (index) {
                            layer.close(index);
                            var ids = [];
                            for (var i = 0; i < data.length; i++) {
                                ids.push(data[i].id);
                            }
                            var tableIds = ids.join(',');
                            var url = serverPath.workflowPath + setter.workflowLinkPath + 'identity/exportXml?tableIds=' + tableIds;
                            window.location.href = url;
                        });
                    }
                    break;
                case 'import':
                    var iframeWin;
                    layer.open({
                        type: 2,
                        title: "导入",
                        area: ['400px', '300px'],
                        content: serverPath.workflowPath + setter.workflowLinkPath + "identity/view/importXml",
                        success: function(layero, index){
                            iframeWin = window[layero.find('iframe')[0]['name']];
                        },
                        end:function () {
                            if(iframeWin.layui.needReload!=undefined){
                                table.reload('serNumber', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('serNumber');
                            }
                        }
                    });
                    break;
            }
        });
        table.on('tool(serNumber)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('确定删除吗？',{skin: 'btn-class'}, function () {
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + 'identity/del',
                            type:'post',
                            dataType: 'json',
                            data: {ids: data.id},
                            success: function (data) {
                                deleteSuccess(data);
                            }
                        })
                    });
                    break;
                case 'edit':
                    addOrUpdate('edit',data.id);
                    break;
                case 'detail':
                    layer.open({
                        type: 2,
                        title: "流水号明细",
                        shade: 0.3,
                        area: ['600px','520px'],
                        content: serverPath.workflowPath+ setter.workflowLinkPath + "identity/view/get?id="+data.id
                    });
                    break;
            }
        })
        common.tableSort("serNumber",{});
        //提取公共方法，删除请求发送成功后的操作
        function deleteSuccess(data) {
            //提示消息
            if (data.code == 0) {
                //删除成功的
                layer.msg(data.msg, {time: 2500, icon: 6});
            }else{
                if (data.data) {
                    var msg = "";
                    if(data.data.serialNumList){
                        msg +="流水号"+data.data.serialNumList+"正在使用中，删除失败！";
                    }
                    if(data.data.delSerialNums){
                        msg +="流水号"+data.data.delSerialNums+"删除成功！";
                    }
                    //既有删除失败的serialNumList，也有删除成功的delSerialNums
                    layer.msg(msg, {
                        time: 2500,
                        icon: 5
                    });
                } else {
                    //删除失败
                    layer.msg(data.msg, {time: 2500, icon: 5});
                }
            }
            //执行重载
            table.reload('serNumber', {
                page: {
                    curr: 1
                }
            });
            totalFilter.init('serNumber');
        }

    });
</script>
</body>
</html>
<link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
<link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">